$(function () {
});
//加载模块
function load(url) {
    //创建异步对象
    var xhr = new XMLHttpRequest();
    //设置请求的类型及url
    xhr.open('get', url,true);
    //post请求一定要添加请求头才行不然会报错
    xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    //发送请求
    xhr.send("");
    xhr.onreadystatechange = function () {
        // 这步为判断服务器是否正确响应
        if (xhr.readyState == 4 && xhr.status == 200) {
            // console.log(xhr.responseText);
            // document.getElementById("content").innerHTML = xhr.responseText;
        }
    };
}
//访问页面完成操作并跳转页面
function sendJump(url,jumpUrl,da,formId,asideId) {
    //创建异步对象
    var xhr = new XMLHttpRequest();
    //设置请求的类型及url
    xhr.open('post', url,true);
    //post请求一定要添加请求头才行不然会报错
    xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    //发送请求
    var data = $("#form").serialize();
    if(formId){
        data = $('#'+formId).serialize();
    }
    if(da){
        data = da;
    }
    console.log(data);
    xhr.send(data);
    xhr.onreadystatechange = function () {
        // 这步为判断服务器是否正确响应
        if (xhr.readyState == 4 && xhr.status == 200) {
            console.log(xhr.responseText);
            var re = JSON.parse(xhr.responseText);
            if(re.code == 200 ){
                alert("操作成功!");
                // if (r==true){
                // }
                setTimeout(window.location.href = jumpUrl,5000);
                return;

            }else{
                alert(re.msg);
                // setTimeout(window.location.href = jumpUrl,5000);

            }
        }
    };
}
function tishi(){
    alert("大家好欢迎来到蚂蚁部落");
}

//分页跳转
function loadPage(flag,formId) {
    document.getElementById("flag").value = flag;
    // $("#flag").valueOf(flag);
    if(formId){
        console.log($("#"+formId).serialize())
        alert(formId);
        $("#"+formId).submit();
        return;
    }
    $("#form").submit();
}

//业务 -- 添加页面 String  基于sendJump
function baojia(offerId) {
    new Dialog({
        title: '报价',
        content: '<h6>请填写报价(单位：元)</h6>' +
            '<p style="margin-top: 50px;"><input style="font-size: 18px;" id="price" type="number" class="ui-input"></p>',
        buttons: [{
            events: function (event) {
                // 按钮禁用
                // event.dialog是当前实例对象
                console.log(event)
                console.log(event.type)
                if(event.type){
                    console.log(document.getElementById("price").value);
                }
                var data = "";
                data = data + "price="+document.getElementById("price").value;
                data = data + "&offerId="+offerId;
                console.log(data);
                sendJump("/offerFlush/insert","/offer/offerWithFlush",data);
                event.dialog.remove();
            }
        }, {}]
    });
}
//业务 --  html    jumpDialog
function checkbaojia(offerId) {
    var data = "offerId="+offerId;
    this.jumpDialog("/offer/offerFlushDetail",data,'报价');
}
function jumpDialog(url,data,title){
    var html = "";
    //创建异步对象
    var xhr = new XMLHttpRequest();
    //设置请求的类型及url
    xhr.open('post', url,true);
    //post请求一定要添加请求头才行不然会报错
    xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    //发送请求
    var data = data;
    // if(data!=null &&data){
    xhr.send(data);
    // }
    xhr.onreadystatechange = function () {
        // 这步为判断服务器是否正确响应
        if (xhr.readyState == 4 && xhr.status == 200) {
            console.log( xhr.responseText);
            html = xhr.responseText;
            if(url.indexOf("add") != -1 || url.indexOf("Add") != -1 || url.indexOf("edit") != -1 ||  url.indexOf("Edit") != -1){
                var a = new Dialog({
                    title: title,
                    content: html,
                    buttons: [{
                        //有操作
                        events: function (event) {
                            document.getElementById("button").click();
                            // 按钮禁用
                            // event.dialog是当前实例对象
                            event.dialog.remove();
                        }

                    }, {}]
                });
            }else{
                new Dialog({
                    title: title,
                    content: html,
                    buttons: [{
                    }, {}]
                });
            }

        }
    };

}
//  编辑  String  基于sendJump
function baojiaEdit(offerId,price) {
    new Dialog({
        title: '报价',
        content: '<h6>请填写报价(单位：元)</h6>' +
            '<p style="margin-top: 50px;"><input style="font-size: 18px;" id="price" placeholder = '+price+' type="number" class="ui-input"></p>',
        buttons: [{
            events: function (event) {
                // 按钮禁用
                // event.dialog是当前实例对象
                console.log(event)
                console.log(event.type)
                if(event.type){
                    console.log(document.getElementById("price").value);
                }
                var data = "";
                data = data + "price="+document.getElementById("price").value;
                data = data + "&offerId="+offerId;
                console.log(data);
                sendJump("/offerFlush/edit","/offer/offerWithFlush",data);
                event.dialog.remove();
            }
        }, {}]
    });
}

//  编辑  String  基于sendJump
function tradePlatePriceEdit(price) {
    new Dialog({
        title: '收盘价',
        content: '<h6>请填写(单位：元)</h6>' +
            '<p style="margin-top: 50px;"><input style="font-size: 18px;" id="price" placeholder = '+price+' type="number" class="ui-input"></p>',
        buttons: [{
            events: function (event) {
                // 按钮禁用
                // event.dialog是当前实例对象
                console.log(event)
                console.log(event.type)
                if(event.type){
                    console.log(document.getElementById("price").value);
                }
                var data = "";
                data = data + "price="+document.getElementById("price").value;
                // data = data + "&id="+userId;
                console.log(data);
                sendJump("/tradePlatePriceEdit","/",data);
                event.dialog.remove();
            }
        }, {}]
    });
}
